﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Creator
{
    public static class DataTableExtension
    {
        /// <summary>
        /// DataTable转换为实体类
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="dt">DataSet</param> 
        /// <returns>实体类</returns>
        public static IList<T> ToEntityList<T>(this DataTable dt) where T : class, new()
        {
            if (dt == null || dt.Rows.Count < 0)
                return default;
            // 返回值初始化
            IList<T> result = new List<T>();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    if (pi.GetMethod.IsVirtual) continue;
                    if (dt.Columns.IndexOf(pi.Name.ToUpper()) != -1 && dt.Rows[j][pi.Name.ToUpper()] != DBNull.Value)
                    {
                        pi.SetValue(_t, dt.Rows[j][pi.Name.ToUpper()], null);
                    }
                    else
                    {
                        pi.SetValue(_t, null, null);
                    }
                }
                result.Add(_t);
            }
            return result;
        }

        public static IList ToEntityList(this DataTable dt,Type targetType)
        {
            if (dt == null || dt.Rows.Count < 0 || targetType == null)
                return default;

            Type listType = typeof(List<>);
            Type elementType = targetType; // 将此更改为所需的类型

            Type specificListType = listType.MakeGenericType(elementType);

            // 使用 Activator.CreateInstance 创建泛型 List<T> 的实例
            var instance = Activator.CreateInstance(specificListType) as IList;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                var _t =  Activator.CreateInstance(targetType);
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    if (pi.GetMethod.IsVirtual) continue;
                    if (dt.Columns.IndexOf(pi.Name.ToUpper()) != -1 && dt.Rows[j][pi.Name.ToUpper()] != DBNull.Value)
                    {
                        try
                        {
                            pi.SetValue(_t, Convert.ChangeType(dt.Rows[j][pi.Name.ToUpper()], Type.GetTypeCode(pi.PropertyType)), null);
                        }
                        catch (Exception)
                        {
                             
                        } 
                    }
                    else
                    {
                        pi.SetValue(_t, null, null);
                    }
                }
                instance.Add(_t);
            }

            return instance;
        }
    }
}
